package com.gigmissions.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MarkerClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.LatLngBounds;
import com.google.gwt.maps.client.overlay.GeoXmlLoadCallback;
import com.google.gwt.maps.client.overlay.GeoXmlOverlay;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Gwtgigmissions implements EntryPoint {
	
	 private static int MAP_BORDER = 5;
	 private static int TOOLPANEL_BORDER = 5;
	 private MapWidget map;
	 private GeoXmlOverlay geoXml = null;
	 
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
	
			
			FlowPanel mainPanel = new FlowPanel();
		 	LatLng centerpoint = LatLng.newInstance(39.509,-98.434);
		    
		 	// Open a map centered on Cawker City, KS USA

		    map = new MapWidget(centerpoint, 2);
		    map.setUIToDefault();

		    GeoXmlOverlay.load("http://gigmissions.appspot.com/getgig.kml",new KmlGigLoadCallback(map,geoXml));
		    HorizontalPanel mapPanel = new HorizontalPanel();
		    mapPanel.setWidth("100%"); 
		    mapPanel.setHeight("100%");
		    mapPanel.add(map); 
		    mapPanel.setSpacing(MAP_BORDER);
		    //Set the size of the map relative to the window 
		    
		    resizeMap(Window.getClientWidth(),Window.getClientHeight()); 
		    Window.addResizeHandler(new ResizeHandler() {
		        public void onResize(ResizeEvent event) {
		        	resizeMap(event.getWidth(), event.getHeight()); 
		        }
		     }
		    );
		    
		    /*check for resizes
		    LatLng center = map.getCenter();
		    map.checkResize();
		    map.setCenter(center);
		    */
		    
		    Button b = new Button("Add Gig...");
		    HorizontalPanel toolPanel = new HorizontalPanel();
		    toolPanel.setWidth("100%"); 
		    toolPanel.setHeight("100%");
		    toolPanel.add(b); 
		    toolPanel.setSpacing(TOOLPANEL_BORDER);
		    
		    
		   
		   // map.setWidth("500px");
		   // map.setHeight("600px");
		    
		    
		    // Add the map to the HTML host page
		    mainPanel.setHeight("100%");
		    mainPanel.setWidth("100%");
		    		
		    mainPanel.add(toolPanel);
		    mainPanel.add(mapPanel);
		    
		    
		    
		    RootPanel.get("mapContainer").add(mainPanel);
		  
		
	}
	
	
	 void resizeMap(int width, int height) {

		 int mapX = map.getAbsoluteLeft(); 
		 int mapY = map.getAbsoluteTop(); 
		 int newWidth = width - mapX - 2 * MAP_BORDER; 
		 int newHeight = height - mapY - 2 * MAP_BORDER; 
		 map.setSize( newWidth + "px", newHeight + "px"); 
	} 
	
	
	private Marker createMarker(LatLng point, final int number) {
	    final Marker marker = new Marker(point);
	    
	    marker.addMarkerClickHandler(new MarkerClickHandler() {
	      public void onClick(MarkerClickEvent event) {
	        InfoWindow info = map.getInfoWindow();
	        Widget parent = map.getParent();
	        
	        info.open(marker,
	            new InfoWindowContent("Marker #<b>" + number + " left = " + parent.getAbsoluteLeft() + "</b>"));
	      }
	    });
	    
	    return marker;
	  }
	
}
